package com.molichuxing.services.infrastructure.service.impl;

import com.alibaba.fastjson.JSON;
import com.molichuxing.services.business.dto.request.create.GoodsExclusiveCreateDto;
import com.molichuxing.services.infrastructure.bean.GoodsExclusiveBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.GoodsExclusiveMapper;
import com.molichuxing.services.infrastructure.dto.response.GoodsExclusiveDto;
import com.molichuxing.services.infrastructure.service.GoodsExclusiveService;
import com.molichuxing.services.property.GoodsExclusiveTypeEnum;
import com.molichuxing.services.property.GoodsPaymentTypeEnum;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 专属里程商品表 服务实现类
 * </p>
 *
 * @author xuelin yi
 * @since 2019-08-12
 */
@Service("goodsProductExclusiveService")
public class GoodsExclusiveServiceImpl implements GoodsExclusiveService {

    private static final Logger log = Logger.getLogger(GoodsSubscribeServiceImpl.class);

    @Resource
    private GoodsExclusiveMapper goodsExclusiveMapper;

    @Override
    public Map<Integer, GoodsExclusiveDto> getGoodsExclusiveByIds(List<Integer> goodsIds) {
        log.info("根据商品id集合查询专属商品信息。入参 goodsIds ： [" + goodsIds + "]");
        List<GoodsExclusiveBean> result = goodsExclusiveMapper.selectBatchIds(goodsIds, null);
        log.info("根据商品id集合查询专属商品信息 出参：queryWrapper： " + JSON.toJSONString(result));
        return Convert.toGoodsProductExclusiveDto(result).stream().collect(Collectors.toMap(GoodsExclusiveDto::getId, a -> a, (k1, k2) -> k1));
    }

    @Override
    public Map<Integer, GoodsExclusiveDto> getGoodsExclusiveByIds(List<Integer> goodsIds,
                                                                  GoodsExclusiveTypeEnum goodsExclusiveType) {

        List<GoodsExclusiveBean> result = goodsExclusiveMapper.selectBatchIds(goodsIds, goodsExclusiveType == null ? null : goodsExclusiveType.getValue());
        return Convert.toGoodsProductExclusiveDto(result).stream().collect(Collectors.toMap(GoodsExclusiveDto::getId, a -> a, (k1, k2) -> k1));
    }

    @Override
    public int getGoodsExclusiveCount(List<Integer> goodsIds, GoodsPaymentTypeEnum goodsPaymentTypeEnum) {
        return 0;
    }

    @Override
    public GoodsExclusiveDto getGoodsExclusiveById(Integer goodsId) {
        log.info("根据商品id查询专属商品信息。入参 goodsId ： [" + goodsId + "]");
        GoodsExclusiveBean result = goodsExclusiveMapper.selectById(goodsId);
        log.info("根据商品id查询专属商品信息 出参：queryWrapper： " + JSON.toJSONString(result));
        return Convert.toGoodsProductExclusiveDto(result);
    }

    @Override
    public int createExclusiveGoodsProduct(GoodsExclusiveCreateDto createDto) {
        log.info("新增专属商品信息列表。 入参 createDto ： [" + createDto + "]");
        GoodsExclusiveBean goodsExclusiveBean = Convert.toExclusiveGoodsProductBean(createDto);
        int result = goodsExclusiveMapper.create(goodsExclusiveBean);
        log.info("新增专属商品信息列表。 出参 result ： [" + result + "]");
        return goodsExclusiveBean.getId();
    }
}
